﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Main_VideoAdd : System.Web.UI.Page
{
    VideoDao vd = new VideoDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsersVo uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            if (uv.Ugroup != "管理员")
            {
                Response.Redirect("../index.aspx");
            }
        }
    }
    protected void btnuplogo_Click(object sender, EventArgs e)
    {
        bool fileOK = false;
        string path = Server.MapPath("../images/video/") + fulogo.FileName;
        if (fulogo.FileName != "")
        {
            if (fulogo.HasFile)
            {
                string fileExtension = Path.GetExtension(fulogo.FileName).ToLower();
                string[] allowerExtensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
                for (int i = 0; i < allowerExtensions.Length; i++)
                {
                    if (fileExtension == allowerExtensions[i])
                    {
                        fileOK = true;
                    }
                }
                if (!fileOK)
                {
                    string script = "<script>alert('图片格式不正确！');</script>";
                    Page.RegisterClientScriptBlock("", script);
                    return;
                }
                else
                {
                    fulogo.PostedFile.SaveAs(path);
                    string script = "<script>alert('上传图片成功！');</script>";
                    txtlogo.Text = fulogo.FileName;
                    Page.RegisterClientScriptBlock("", script);
                }
            }
            else
            {
                string script = "<script>alert('上传失败，格式不允许！');</script>";
                Page.RegisterClientScriptBlock("", script);
            }
        }
        else
        {
            string script = "<script>alert('上传不能为空！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        if (ddltype.SelectedValue == "0")
        {
            string script = "<script>window.alert('请选择所属类型！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        VideoVo vv = new VideoVo();
        vv.Vname = txtname.Text;
        vv.Vurl = "../images/video/" + txtlogo.Text;
        vv.Vlink = txtlink.Text;
        vv.Vtype = ddltype.SelectedValue;
        Exception ex = vd.addVideo(vv);
        if (ex == null)
        {
            string script = "<script>window.alert('添加成功！');location.href='VideoData.aspx';</script>";
            Page.RegisterClientScriptBlock("", script);
        }
        else
        {
            string script = "<script>window.alert('添加失败！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }
}
